//<p>给定两个整数，分别表示分数的分子&nbsp;<code>numerator</code> 和分母 <code>denominator</code>，以 <strong>字符串形式返回小数</strong> 。</p>
//
//<p>如果小数部分为循环小数，则将循环的部分括在括号内。</p>
//
//<p>如果存在多个答案，只需返回 <strong>任意一个</strong> 。</p>
//
//<p>对于所有给定的输入，<strong>保证</strong> 答案字符串的长度小于 <code>10<sup>4</sup></code> 。</p>
//
//<p>&nbsp;</p>
//
//<p><strong>示例 1：</strong></p>
//
//<pre>
//<strong>输入：</strong>numerator = 1, denominator = 2
//<strong>输出：</strong>"0.5"
//</pre>
//
//<p><strong>示例 2：</strong></p>
//
//<pre>
//<strong>输入：</strong>numerator = 2, denominator = 1
//<strong>输出：</strong>"2"
//</pre>
//
//<p><strong>示例 3：</strong></p>
//
//<pre>
//<strong>输入：</strong>numerator = 4, denominator = 333
//<strong>输出：</strong>"0.(012)"
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示：</strong></p>
//
//<ul> 
// <li><code>-2<sup>31</sup> &lt;=&nbsp;numerator, denominator &lt;= 2<sup>31</sup> - 1</code></li> 
// <li><code>denominator != 0</code></li> 
//</ul>
//
//<div><li>👍 494</li><li>👎 0</li></div>

#include <bits/stdc++.h>
using namespace std;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    string fractionToDecimal(int a_, int b_) {
        string ans;
        if((a_ < 0 && b_ > 0) || (a_ > 0 && b_ < 0)) {
            ans.append("-");
        }
        long a = abs((long long)(a_));
        long b = abs((long long)(b_));
        ans.append(to_string(abs(a / b)));
        a = a % b;
        if(a == 0) return ans;
        string xs = "";
        unordered_map<int, int> map;
        int i = 0;
        while (a > 0) {
            if(map.count(a)) {
                xs.insert(map[a], "(");
                xs.append(")");
                break;
            }
            map[a] = i;
            a *= 10;
            xs.append(to_string(a/b));
            a = a % b;
            i++;
        }
        return ans.append(".").append(xs);
    }
};
//leetcode submit region end(Prohibit modification and deletion)


int main() {
    Solution s;
    cout<<s.fractionToDecimal(7, -12)<<endl;
    //cout<<endl;
}